package com.sunny.miaosha.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.miaosha.domain.User;
import com.sunny.miaosha.result.CodeMsg;
import com.sunny.miaosha.result.Result;
import com.sunny.miaosha.service.UserService;

@Controller
public class DemoController {
	@Autowired
	private UserService userService;

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }
    
    // 1.rest api json输出 2.页面
    
    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello() {
    	return Result.success("hello");
    }
     
    @RequestMapping("/helloError")
    @ResponseBody
     public Result<String> helloError() {
    	 return Result.error(CodeMsg.SERVER_ERROR);
     }
    
    @RequestMapping("/thymeleaf")
    @ResponseBody
     public String thymeleaf(Model model) {
    	model.addAttribute("name", "david");
    	return "hello";
     }
    
    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet() {
    	User user = userService.getById(1);
    	return Result.success(user);
    }
    
    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx() {
    	userService.dbTx();
    	return Result.success(true);
    }
}